package com.example.swd1.gudong.main.message;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.swd1.gudong.R;
import com.example.swd1.gudong.main.MainActivity;
import com.example.swd1.gudong.model.MessageItem;
import com.example.swd1.gudong.view.ReFreshView;

import java.util.ArrayList;
import java.util.List;

//==================================================================================================================
//   Author :  Shile Wu
//   Date  :  16-8-19
//   Role :    xx
//   Reference documents : xx
//==================================================================================================================
public class MessageFragment extends android.support.v4.app.Fragment implements AdapterView.OnItemClickListener {

    private ListView messageList;
    private MessageListViewAdapter adapter;
    private List<MessageItem> messageItemList;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (messageItemList == null){
            messageItemList = new ArrayList<>();
            adapter = new MessageListViewAdapter(getActivity(), messageItemList);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_message, container, false);
        messageList = (ListView) view.findViewById(R.id.message_list);
        messageList.setAdapter(adapter);
        messageList.setOnItemClickListener(this);
        return view;
    }

    public void setMessageList(MessageItem item, Context context) {
        if(messageItemList != null){
            for (MessageItem messageItem : messageItemList) {
                if (messageItem.getMessageTitle().equals(item.getMessageTitle())) {
                    messageItem.setUnReadNum(messageItem.getUnReadNum() + 1);
                    messageItem.setMessageContent(item.getMessageContent());
                    messageItem.setMessageTime(item.getMessageTime());
                    adapter.notifyDataSetChanged();
                    return;
                }
            }
        }else {
            messageItemList = new ArrayList<>();
            adapter = new MessageListViewAdapter(context, messageItemList);
        }
        messageItemList.add(item);
        adapter.notifyDataSetChanged();
    }

    public void setMessageList(List<MessageItem> list,Context context){
        if (messageItemList == null){
            messageItemList = new ArrayList<>();
            adapter = new MessageListViewAdapter(context, messageItemList);
            messageItemList.addAll(list);
            adapter.notifyDataSetChanged();
        }
    }

    /*
    * when click the list's item,reset the unread message number
    * */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        ((MainActivity)getActivity()).readMessageNum(messageItemList.get(position).getUnReadNum());
        messageItemList.get(position).setUnReadNum(0);
        adapter.notifyDataSetChanged();
    }

    public List<MessageItem> getMessageItemList(){
        return messageItemList;
    }

}
